package com.leenmvc.core.security.service.core;

import cn.hutool.core.io.FileUtil;
import com.leenmvc.core.base.BaseService;
import com.leenmvc.core.dao.communal.Wrapper;
import com.leenmvc.core.security.entity.core.SysVue;
import com.leenmvc.core.security.entity.core.SysVueData;
import com.leenmvc.core.security.entity.core.TitleAndi18n;
import com.leenmvc.core.utils.unique.SnowFlake;
import com.leenmvc.core.base.result.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: JohnCoding
 * @Description: 前端配置页面的业务处理器
 * @date: 2020/3/16 10:22
 */
@Service
public class SysVueService extends BaseService<SysVue> {


    @Autowired
    SysVueDataService sysVueDataService;

    @Override
    public <T> boolean insert(T t) {
        SysVue sysVue = (SysVue) t;
        sysVue.setId(SnowFlake.generateId());
//        writeToJs(sysVue);
        List<SysVueData> i18n = sysVue.getI18n();
        for (SysVueData vueData : i18n) {
            vueData.setVid(sysVue.getId());
            sysVueDataService.insert(vueData);
        }
        return super.insert(t);
    }

    @Override
    public <T> boolean update(T t) {
        SysVue newsysVue = (SysVue) t;
        //查询旧的
        SysVue oldSysVue= get("id", newsysVue.getId());
        //旧的
        List<SysVueData> oldsysVueData= sysVueDataService.getList("vid", newsysVue.getId());
        oldSysVue.setI18n(oldsysVueData);
//        deleteByJs(oldSysVue);
//        writeToJs(newsysVue);
        oldsysVueData.forEach(x ->{
            sysVueDataService.deleteById(x.getId());
        });
        List<SysVueData> newi18n = newsysVue.getI18n();
        newi18n.forEach(x -> {
            x.setVid(oldSysVue.getId());
            sysVueDataService.insert(x);
        });
        return super.update(t);
    }

    @Override
    public int deleteByIds(Object[] ids) {
        List<Long> vueDataIds = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            SysVue sysVue = get("id", ids[i]);
            List<SysVueData> vueData = sysVueDataService.getList("vid", ids[i]);
            sysVue.setI18n(vueData);
//            deleteByJs(sysVue);
            vueData.forEach(x -> {
                vueDataIds.add(x.getId());
            });
        }
        sysVueDataService.deleteByIds(vueDataIds.toArray());
        return super.deleteByIds(ids);
    }


    /**
     * 把内容写入js文件
     * @param sysVue
     */
    public void writeToJs(SysVue sysVue) {
        //读取之前的内容
        List<String> strings = FileUtil.readUtf8Lines(getJsFile(sysVue.getFilePath()));
        ArrayList<String> list = new ArrayList<>();
        //把之前的存好
        list.addAll(strings);
        //新添加的内容写入
        List<SysVueData> i18n = sysVue.getI18n();
        list.add("t."+sysVue.getI18nTitle()+" = {}");
        i18n.forEach(x -> {
            list.add("t."+sysVue.getI18nTitle()+"."+x.getLabel()+" = "+x.getValue());
        });
        FileUtil.writeUtf8Lines(list, getJsFile(sysVue.getFilePath()));
    }


    /**
     * 按照匹配规则删除js文件内容
     *
     * @param sysVue
     */
    public void deleteByJs(SysVue sysVue) {
        //获取文件内容
        List<String> strings = FileUtil.readUtf8Lines(getJsFile(sysVue.getFilePath()));
        ArrayList<String> list = new ArrayList<>();
        for (String string : strings) {
            if(string.indexOf(sysVue.getI18nTitle()) == -1){
                list.add(string);
            }
        }
        FileUtil.writeUtf8Lines(list, getJsFile(sysVue.getFilePath()));
    }

    public void updateToJs(SysVue sysVue) {
        //读取文件内容(一行一行读)
        List<String> strings = FileUtil.readUtf8Lines(getJsFile(sysVue.getFilePath()));
        //添加
        strings.add(sysVue.getI18nTitle() + " = " + sysVue.getI18n());
        FileUtil.writeUtf8Lines(strings, getJsFile(sysVue.getFilePath()));
    }

    /**
     * 获取js文件
     *
     * @return
     */
    public File getJsFile(String filePath) {
        File file = FileUtil.file(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    public TitleAndi18n getI18n() {
        TitleAndi18n titleAndi18n = new TitleAndi18n();
        List<SysVueData> sysVueData = sysVueDataService.getAll();
        List<Object> list = listBySqlKey("getI18nTitle");
        titleAndi18n.setI18nTile(list);
        titleAndi18n.setI18n(sysVueData);
        return titleAndi18n;
    }

    @Override
    public <T> PageData<T> getPage(Wrapper wrapper) {
        PageData pageData = super.getPage(wrapper);
        List<SysVue> data = pageData.getData();
        data.forEach(x->{
            List<SysVueData> sysVueData = sysVueDataService.getList("vid", x.getId());
            x.setI18n(sysVueData);
        });
        pageData.setData(data);
        return pageData;
    }
}
